//
//  SGTabBarController.m
//  SG
//
//  Created by FISH on 2018/4/18.
//  Copyright © 2018年 yimindai. All rights reserved.
//

#import "SGTabBarController.h"
#import "JDLTabBar.h"
#import "UITabBar+CustomBadge.h"
@interface SGTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic,strong) NSMutableArray * VCS;//tabbar root VC

@end

@implementation SGTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    //初始化tabbar
    [self setUpTabBar];
    //添加子控制器
    [self setUpAllChildViewController];
}
#pragma mark ————— 初始化TabBar —————
-(void)setUpTabBar{
    //设置背景色 去掉分割线
    [self setValue:[JDLTabBar new] forKey:@"tabBar"];
    [self.tabBar setBackgroundColor:[UIColor whiteColor]];
    [self.tabBar setBackgroundImage:[UIImage new]];
    //通过这两个参数来调整badge位置
    //    [self.tabBar setTabIconWidth:29];
    //    [self.tabBar setBadgeTop:9];
}

#pragma mark - ——————— 初始化VC ————————
-(void)setUpAllChildViewController{
    _VCS = @[].mutableCopy;
    HomeViewController *HomeVC = [[HomeViewController alloc]init];
    [self setupChildViewController:HomeVC title:@"附近" imageName:@"tabBar_essence_icon" seleceImageName:@"tabBar_essence_click_icon"];
    
    ManageViewController *ManageVC = [ManageViewController new];
    [self setupChildViewController:ManageVC title:@"需求管理" imageName:@"tabBar_demand_icon" seleceImageName:@"tabBar_demand_click_icon"];
    
//    MiddleViewController *MiddleVC = [[MiddleViewController alloc]init];
//    [self setupChildViewController:MiddleVC title:@"" imageName:@"mine" seleceImageName:@"mine"];
    
    SkillsViewController *SkillsVC = [[SkillsViewController alloc]init];
    [self setupChildViewController:SkillsVC title:@"技能管理" imageName:@"tabBar_skills_icon" seleceImageName:@"tabBar_skills_click_icon"];
    
    PersonalViewController *PersonalVC = [[PersonalViewController alloc]init];
    [self setupChildViewController:PersonalVC title:@"个人中心" imageName:@"tabBar_personal_icon" seleceImageName:@"tabBar_personal_click_icon"];
    
    self.viewControllers = _VCS;
}

-(void)setupChildViewController:(UIViewController*)controller title:(NSString *)title imageName:(NSString *)imageName seleceImageName:(NSString *)selectImageName{
    if([selectImageName isEqualToString:@"mine"])
    {//top bottom相反数
        controller.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    }
    controller.title = title;
    controller.tabBarItem.title = title;//跟上面一样效果
    controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //未选中字体颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:KBlackColor,NSFontAttributeName:FONTSIZE(10.0f)} forState:UIControlStateNormal];
    
    //选中字体颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:KRedColor,NSFontAttributeName:FONTSIZE(10.0f)} forState:UIControlStateSelected];
    //包装导航控制器
//    SGNavigationController *nav = [[SGNavigationController alloc]initWithRootViewController:controller];
    
    //    [self addChildViewController:nav];
    [_VCS addObject:controller];
    
}
// 控制哪些ViewController的标签栏能被点击

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[MiddleViewController class]]) {
        
    }
    else
    {
        [Singleton ShareDatabase].viewController = viewController;
    }
    return YES;
    
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    DLog(@"选中 %ld",tabBarController.selectedIndex);
    [self.tabBar setBadgeStyle:kCustomBadgeStyleNumber value:10 atIndex:0];
    [self.tabBar setBadgeStyle:kCustomBadgeStyleNumber value:10 atIndex:1];
    
    [self.tabBar setBadgeStyle:kCustomBadgeStyleNumber value:10 atIndex:2];

    [self.tabBar setBadgeStyle:kCustomBadgeStyleNumber value:10 atIndex:3];

}

-(void)setRedDotWithIndex:(NSInteger)index isShow:(BOOL)isShow{
    if (isShow) {
        [self.tabBar setBadgeStyle:kCustomBadgeStyleRedDot value:10 atIndex:index];
    }else{
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNone value:0 atIndex:index];
    }
    
}

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
